package com.xueblog;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

public class TestJava {
    public static List<OrganizationInfoMx> orgList = new ArrayList<>();

    public static void main(String[] args) {//在这运行 看结果、
        // 添加示例数据
        orgList.add(new OrganizationInfoMx("三年级"));
        orgList.add(new OrganizationInfoMx("一年级"));
        orgList.add(new OrganizationInfoMx("四年级"));
        orgList.add(new OrganizationInfoMx("二年级"));

        // 调用sortOrg方法排序
//        List<OrganizationInfoMx> newList = new TestJava().sortOrg(orgList);
//        int zy = new TestJava().DHInOutTOHk("张钰-入");
//        int xjy = new TestJava().DHInOutTOHk("薛佳音");
//        System.out.println("zy" + zy + "---" + "xjy" + xjy);
//
//        // 打印排序后的结果
//        for (OrganizationInfoMx org : newList) {
//            System.out.println(org.orgName);
//        }

//        long totalPage = new TestJava().getTotalPage(5L, 20L);
//        long totalPage2 = new TestJava().getTotalPage(5L, 21L);
//        System.out.println(totalPage + "---" + totalPage2);
//        HashSet<Integer> zs = new TestJava().getZS(5);
//        zs.forEach(System.out::println);


//        System.out.println(DateTimeUtils.getCurrDateTime());
//        System.out.println(DateTimeUtils.getCurrDate8());
//
//        String s = new TestJava().DHTime2ISO8601(DateTimeUtils.getCurrDateTime());
//        System.out.println(s);

//        System.out.println(TestJava.timeIsToday("2023/10/27 12:30:00"));

        System.out.println(TestJava.getFormat("2023/11/1 0:00:00"));


    }

    private List<OrganizationInfoMx> sortOrg(List<OrganizationInfoMx> orgList) {
//        return orgList.stream().sorted(Comparator.comparingInt(o -> getNianJi(o.getOrgName().split("年")[0]))).collect(Collectors.toList());
        return orgList.stream().sorted((o1, o2) -> {
            int o1h = o1.hashCode();
            System.out.println(o1.getOrgName() + o1h);
            int o2h = o2.hashCode();
            System.out.println(o2.getOrgName() + o2h);
            return Integer.compare(o1h, o2h);
        }).collect(Collectors.toList());

    }

    private Integer getNianJi(String str) {
        switch (str) {
            case "一":
                return 1;
            case "二":
                return 2;
            case "三":
                return 3;
            case "四":
                return 4;
            case "五":
                return 5;
            case "六":
                return 6;
            case "七":
                return 7;
            case "八":
                return 8;
            default:
                return 9;
        }
    }

    private static Integer DHInOutTOHk(String acsChannelNam) {
        //todo 名称中结尾是  -入 则识别为入园的, 其他识别为出园的
        if (acsChannelNam.lastIndexOf("-入") == (acsChannelNam.length() - 2)) {//入园
            return 1;
        } else {
            return 2;
        }
    }

    public static class OrganizationInfoMx {
        private String orgName;

        public OrganizationInfoMx(String orgName) {
            this.orgName = orgName;
        }

        public String getOrgName() {
            return orgName;
        }
    }

    /**
     * todo 计算总页码
     *
     * @param pageSize 页容量
     * @param total    总条数
     * @return 总页码
     */
    private long getTotalPage(Long pageSize, Long total) {
        return (int) (total % pageSize) == 0 ? total / pageSize : (total / pageSize + 1);
    }

    private HashSet<Integer> getZS(int x) {
        HashSet<Integer> ints = new HashSet<>();
        for (int i = 0; i <= x; i++) {
            if (i == 1 || i == 2) {
                ints.add(i);
            } else {
                for (int j = 2; j < i; j++) {
                    if (i % j == 0) {
                        break;
                    }
                    ints.add(i);
                }
            }
        }
        return ints;
    }

    /**
     * 2021-11-23 09:29:51  -> iso8601
     *
     * @param time
     * @return
     */
    private String DHTime2ISO8601(String time) {
//        todo
        String[] s = time.split(" ");
        String s2 = s[0] + "T" + s[1] + "+08:00";
        return s2;
    }

    /**
     *  2021-11-23 09:29:51  -> iso8601
     * @param time
     * @return
     */
//    private String DHTime2ISO8601(String time){
//        //设置日期格式
//        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        DateFormat dfISO = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
//        df.setTimeZone(tz);
//        dfISO.setTimeZone(tz);
//        Date parse = null;
//        try {
//            parse = df.parse(time);
//        } catch (ParseException e) {
////            log.error("日期转换失败", e);
//        }
//        if (parse != null) {
//            return dfISO.format(parse);
//        } else {
//            return "";
//        }
//    }

    /**
     * @param happenTime 任意格式的时间字符串
     * @return happenTime在不在今天   大于等于今天00:00:00  小于今天23:59:59
     */
    public static boolean timeIsToday(String happenTime) {
        Date date = DateUtil.parse(happenTime);
        String format = DateUtil.format(date, "yyyy/MM/dd");
        String format2 = DateUtil.format(DateUtil.date(), "yyyy/MM/dd");
        return format.equals(format2);
    }

    public static Date getFormat(String dateTime){
//        2023/11/1 0:00:00
        return DateUtil.parse(dateTime);
    }
    private String getSId() {
        // 随机 9流水码
        DateTime now = new DateTime();
        String mmddhh = now.toString("MMDDHH");
        String randomString = getRandomString();
        return mmddhh + randomString;
    }

    public String getRandomString() {
        //创建缓存，默认60分钟过期
        TimedCache<String, String> timedCache = CacheUtil.newTimedCache(1000 * 60 * 60);
        while(true){
            String randomString = RandomUtil.randomString(3);
            if (!timedCache.containsKey(randomString)) {
                timedCache.put(randomString,"1");
                return randomString;
            }
        }
    }
}
